package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.sun.jna.platform.unix.solaris.LibKstat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismFood;
import com.ruoyi.system.service.ITourismFoodService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 美食Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/food")
public class TourismFoodController extends BaseController
{
    @Autowired
    private ITourismFoodService tourismFoodService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询美食列表
     */
    @PreAuthorize("@ss.hasPermi('system:food:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismFood tourismFood)
    {
        startPage();
        List<TourismFood> list = tourismFoodService.selectTourismFoodList(tourismFood);
        return getDataTable(list);
    }

    /**
     * 导出美食列表
     */
    @PreAuthorize("@ss.hasPermi('system:food:export')")
    @Log(title = "美食", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismFood tourismFood)
    {
        List<TourismFood> list = tourismFoodService.selectTourismFoodList(tourismFood);
        ExcelUtil<TourismFood> util = new ExcelUtil<TourismFood>(TourismFood.class);
        return util.exportExcel(list, "food");
    }

    /**
     * 获取美食详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:food:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(tourismFoodService.selectTourismFoodById(id));
    }

    /**
     * 新增美食
     */
    @PreAuthorize("@ss.hasPermi('system:food:add')")
    @Log(title = "美食", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismFood tourismFood)
    {
        return toAjax(tourismFoodService.insertTourismFood(tourismFood));
    }

    /**
     * 修改美食
     */
    @PreAuthorize("@ss.hasPermi('system:food:edit')")
    @Log(title = "美食", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismFood tourismFood)
    {
        return toAjax(tourismFoodService.updateTourismFood(tourismFood));
    }

    /**
     * 删除美食
     */
    @PreAuthorize("@ss.hasPermi('system:food:remove')")
    @Log(title = "美食", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tourismFoodService.deleteTourismFoodByIds(ids));
    }


    /**
     * 根据美食名称查询美食信息
     * @param map
     * @return
     */
    @PostMapping("/selectFoodByFood")
    public AjaxResult selectFoodByFood(@RequestBody Map map){
        String foodName = (String) map.get("foodName");
        return tourismFoodService.selectFoodByFood(foodName);
    }


    /**
     * 查询相关的美食的信息
     * @param map
     * @return
     */
    @PostMapping("/selectFoodAndRestaurant")
    public AjaxResult selectFoodAndRestaurant(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            Integer foodId = (Integer) map.get("foodId");
            return tourismFoodService.selectFoodAndRestaurant(foodId,userId);
        }else {
            Integer userId = null;
            Integer foodId = (Integer) map.get("foodId");
            return tourismFoodService.selectFoodAndRestaurant(foodId,userId);
        }
    }

    /**
     * 查询特色美食的信息
     * @param map
     * @return
     */
    @PostMapping("/selectFoodByStatus")
    public AjaxResult selectFoodByStatus(@RequestBody Map map){
        String foodName = (String) map.get("foodName");
        return tourismFoodService.selectFoodByStatus(foodName);
    }
}
